import java.awt.Dimension;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;
import javax.swing.Timer;



public class Futbol extends Applet implements Runnable, MouseListener, MouseMotionListener, ActionListener {



	static final long serialVersionUID = 0L;
	Image campo, pantalla;
	Graphics aux;
	Balón balones[], balón;
	int a[] = {307, 200, 103, 30};//Direcciones iniciales nº entre 0 y 2Pi
	int x[] = {0, 539, 539, 0};
	int y[] = {0, 0, 306, 306};
	int distX, distY, segundos, decimas, retardo;
	Timer timer;
	Thread t;
	boolean juegoIniciado, balónCapturado;
	
	public void init(){
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		Dimension d = new Dimension(640, 407);
		campo = getImage(getCodeBase(), "img/campo.png");
		pantalla = createImage(640, 407);
		aux = pantalla.getGraphics();
		balón = new Balón(getImage(getCodeBase(), "img/balon0.png"),
				270, 153, 0, 0, this);
		balones = new Balón[4];
		for (int i=0; i<balones.length; i++)
			balones[i] = new Balón(getImage(getCodeBase(), "img/balon" + i + ".png"), 
					x[i], y[i], 5, Math.PI * a[i] / 180, this);
		juegoIniciado = balónCapturado = false;
		this.setSize(d);
		timer = new Timer(100, this);
		
	}
	
	
	public void start(){

	}
	
	public void run(){
		Thread t = Thread.currentThread();
		while(this.t == t){
			for (int i=0; i<balones.length; i++){
				balones[i].mover(0, 0, 640, 407);
				if (balón.colision(balones[i])){
					finPartida();
					break;
				}
			}
			repaint();
			try {
				Thread.sleep(retardo);
			} catch(Exception e){}
		}
	}
	
	public void update(Graphics g){
		paint(g);
	}
	
	public void paint(Graphics g){
		aux.drawImage(campo, 0, 0, this);
		for (int i=0; i<balones.length; i++)
			balones[i].paint(aux);
		balón.paint(aux);
		if (juegoIniciado)
			aux.drawString(segundos + ":" + decimas, 50, 50);
		g.drawImage(pantalla, 0, 0, this);

	}
	
	
	public void mouseDragged(MouseEvent e) {
		if (juegoIniciado && balónCapturado){
			boolean colision = balón.moverXY(e.getX() + distX, e.getY() + distY, 639, 406);
		repaint();
		if(colision)
			finPartida();
		else {
			for (int i=0; i<balones.length; i++)
				if (balón.colision(balones[i])){
					finPartida();
					break;
				}
			}
		}
	}
		public void finPartida (){
			juegoIniciado = balónCapturado = false;
			t = null;
			timer.stop();
			JOptionPane.showMessageDialog(this,
					"Has conseguido aguantar " + segundos + " segundos" + decimas + " decimas",
					"Fin de la partida",
					JOptionPane.INFORMATION_MESSAGE);
			balón.moverXY(270, 153, 639, 406);
			for (int i=0; i<balones.length; i++)
				balones[i].moverXY(x[i], y[i], 639, 406);
			repaint();

		}
	public void mouseMoved(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {//Implementar set direccion en balon sobre vx y vy
		if(e.getButton()== MouseEvent.BUTTON1){
			int dx = e.getX() - balón.getCX();
			int dy = e.getY() - balón.getCY();
			int d = (int)Math.round(Math.sqrt(dx * dx + dy * dy));
			if ( d <= balón.getRadio()){
				if (!juegoIniciado){
					segundos = decimas = 0;
					retardo = 50;
					juegoIniciado = true;
					t = new Thread(this);
					if (t != null){
						t.start();
						timer.start();
					}
				}
				balónCapturado = true;
				distX = balón.getX() - e.getX();
				distY = balón.getY() - e.getY();
			}
		}
	}
	public void mouseReleased(MouseEvent e) {
		balónCapturado = false;
	}

	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == timer){
			decimas++;
			if (decimas == 10){
				segundos++;
				decimas=0;
				if (retardo < 5) retardo--;
			}
		}
	}
}
